<?php
require_once("DBConfig.php");
require_once("User.php");

class Database{
	
	private $m_mysqli = NULL;
	
	public function Connect(DBConfig $dbconfig){
		
		$this->m_mysqli = new mysqli($dbconfig->m_host, $dbconfig->m_user, $dbconfig->m_pass, $dbconfig->m_db);
		
		if($this->m_mysqli->connect_errno){
			throw new Exception("An error occured while trying to connect to the database!");
		}
		
		$this->m_mysqli->set_charset("utf-8");
		
		return true;
	}
	
	public function Close(){
		$this->m_mysqli->close();
	}
	
	public function Prepare($sql){
		$ret = $this->m_mysqli->prepare($sql);
		
		if(!$ret){
			throw new Exception("There was a problem preparing your sql command!");
		}
		
		return $ret;
	}
	/**
	 * Removes a User from the database
	 * @param $username = Username to Insert
	 */	
	public function DeleteUser($userid){
		$sqlquery = "DELETE FROM users WHERE UserID = ?";
		$stmt = $this->Prepare($sqlquery);
		$stmt->bind_param('i', $userid);
		
		if(!$stmt->execute()){
			throw new Exception("Executing your insert statement failed!");
		}
		$stmt->store_result();
		
		if($stmt->affected_rows != 1){
			throw new Exception("Your insert statement have no affected rows!");
		}
		
		$stmt->close();
		return true;
	}
	/**
	 * Searches the Database for the username and password sent into the function
	 * @param $username = Username to search for in the Database.
	 * @param $password = Password to match with the username.
	 * @return 1 or 0 rows depending if the user can be found
	 */
	public function SelectUser($username, $password){
		$sqlquery = "SELECT * FROM users WHERE UserName = ? AND Password = ?";
		$stmt = $this->Prepare($sqlquery);
		$stmt->bind_param('ss', $username, $password);

		$stmt->execute();
		$stmt->store_result();

		$rows = $stmt->num_rows;
		$stmt->close();
		
		return $rows;
	}
	public function SelectUsers(){
		$sqlquery = "SELECT UserID, UserName FROM users ORDER BY UserID";

		$stmt = $this->Prepare($sqlquery);

		$stmt->execute();
		
		if ($stmt->bind_result($userid, $username) == FALSE) {
            throw new Exception($this->mysqli->error);
        }
		$stmt->bind_result($userid, $username);
		
        $ret = new UserArray();
		
		 while ($stmt->fetch()) {
              $ret->add(new User($userid, $username));
         }
		 
		 $stmt->close();
		 
		 return $ret;
		
		
	}
	public function FindUser($userid){
		$sqlquery = "SELECT * FROM users WHERE UserID = ?";
		$stmt = $this->Prepare($sqlquery);
		$stmt->bind_param('i', $userid);

		$stmt->execute();
		$stmt->store_result();

		$rows = $stmt->num_rows;
		$stmt->close();
		
		return $rows;
	}
	public function FindUserName($username){
		$sqlquery = "SELECT UserName FROM users WHERE UserName = ?";
		$stmt = $this->Prepare($sqlquery);
		$stmt->bind_param('s', $username);
		
		$stmt->execute();
		$stmt->store_result();
		
		$rows = $stmt->num_rows;
		$stmt->close();
		
		return $rows;
	}
	/**
	 * Inserts a User into the database
	 * @param $username = Username to Insert
	 * @param $password = Password to the User
	 */
	public function InsertUser($username, $password){
		$sqlquery = "INSERT INTO users (UserName, Password) VALUES (?, ?)";
		$stmt = $this->Prepare($sqlquery);
		$stmt->bind_param('ss', $username, $password);
		
		if(!$stmt->execute()){
			throw new Exception("Executing your insert statement failed!");
		}
		$stmt->store_result();
		
		if($stmt->affected_rows != 1){
			throw new Exception("Your insert statement have no affected rows!");
		}
		
		$stmt->close();
	}
	
	public function DBTest(){
		
		$dbconfig = new DBConfig();
		if($this->Connect($dbconfig))
		{
			$this->SelectUser("Select * FROM users WHERE UserName = 'Derpy' AND Password = 'Hooves'");
			$this->Close();
			return true;
		}
	}
	
	
}
?>